package com.aner.test.domain;

import com.aner.test.utils.I18nUtils;
import com.aner.tester.utils.text.StringUtils;

import java.util.HashMap;

/**
 * response result object
 * 
 * @author aner
 */
public class CommonResult extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /**
     *  response code
     */
    public static final String CODE_TAG = "code";

    /**
     *  response message
     */
    public static final String MSG_TAG = "msg";

    /**
     *  response data
     */
    public static final String DATA_TAG = "data";

    /**
     * response timestamp
     */
    public static final String TIMESTAMP_TAG = "timestamp";

    public CommonResult()
    {
    }

    /**
     * 
     * @param code
     * @param msg
     */
    public CommonResult(int code, String msg)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(TIMESTAMP_TAG, System.currentTimeMillis());
    }

    /**
     * 
     * @param code
     * @param msg
     * @param data
     */
    public CommonResult(int code, String msg, Object data)
    {
        this(code, msg);
        if (StringUtils.isNotNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    /**
     * 
     * @return
     */
    public static CommonResult success()
    {
        return CommonResult.success(I18nUtils.message("operation.success"));
    }

    /**
     * 
     * @return
     */
    public static CommonResult success(Object data)
    {
        return success(I18nUtils.message("operation.success"), data);
    }

    /**
     * 
     * @param msg
     * @return
     */
    public static CommonResult success(String msg)
    {
        return CommonResult.success(msg, null);
    }

    /**
     * 
     * @param msg
     * @param data
     * @return
     */
    public static CommonResult success(String msg, Object data)
    {
        return new CommonResult(200, msg, data);
    }

    /**
     * 
     * @return
     */
    public static CommonResult error()
    {
        return CommonResult.error(I18nUtils.message("operation.error"));
    }

    /**
     * 
     * @param msg
     * @return
     */
    public static CommonResult error(String msg)
    {
        return CommonResult.error(msg, null);
    }

    /**
     * 
     * @param msg
     * @param data
     * @return
     */
    public static CommonResult error(String msg, Object data)
    {
        return new CommonResult(404, msg, data);
    }

    /**
     * 
     * @param code
     * @param msg
     * @return
     */
    public static CommonResult error(int code, String msg)
    {
        return new CommonResult(code, msg, null);
    }
}
